package com.rt.gptnext.common.bean;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.SystemClock;
import com.rt.gptnext.common.exception.IResultCode;
import com.rt.gptnext.common.exception.ResultCode;
import com.rt.gptnext.common.utils.CommonUtil;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * @author gaoyiyang
 **/
@Data
@NoArgsConstructor
public class HttpResult<T> {

    private String code;

    private String message;

    private T data;

    private boolean success;

    private long timestamp = SystemClock.now();

//    private String requestId;

    public HttpResult(IResultCode code) {
        this.code = code.getCode();
        this.message = code.getMessage();
//        this.requestId = RequestContext.getRequestId();
    }

    public static <T> HttpResult<T> success() {
        HttpResult<T> result = new HttpResult<>(ResultCode.SUCCESS);
        result.success = true;
        return result;
    }

    public static <T> HttpResult<T> success(T data) {
        HttpResult<T> result = new HttpResult<>(ResultCode.SUCCESS);
        result.data = data;
        result.success = true;
        return result;
    }

    public static <T, S> HttpResult<T> success(S data, Class<T> targetClass) {
        HttpResult<T> result = new HttpResult<>(ResultCode.SUCCESS);
        result.data = BeanUtil.copyProperties(data, targetClass);
        result.success = true;
        return result;
    }

    public static <T, S> HttpResult<List<T>> success(List<S> data, Class<T> targetClass) {
        HttpResult<List<T>> result = new HttpResult<>(ResultCode.SUCCESS);
        result.data = CommonUtil.copyList(data, targetClass);
        result.success = true;
        return result;
    }

    public static <T> HttpResult<T> failure() {
        return new HttpResult<>(ResultCode.SYSTEM_ERROR);
    }

    public static <T> HttpResult<T> failure(String message) {
        HttpResult<T> result = new HttpResult<>(ResultCode.SYSTEM_ERROR);
        result.message = message;
        return result;
    }

    public static <T> HttpResult<T> failure(IResultCode resultCode, String message) {
        HttpResult<T> result = new HttpResult<>(resultCode);
        result.message = message;
        return result;
    }

    public static <T> HttpResult<T> failure(IResultCode resultCode) {
        return new HttpResult<>(resultCode);
    }

}
